<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <title>PhpBy个人轻博客 - 首页</title>
    <link rel="dns-prefetch" href="http://s.w.org/">
    <link rel="stylesheet" id="main-css" href="/static/index/css/index.css" type="text/css" media="all">
    <meta name="keywords" content="PhpBy个人轻博客">
    <meta name="description" content="PhpBy个人轻博客">
</head>
<body class="page-template-default page page-id-2119 uimo ui-c3">
<section class="container">
    <header class="header">
        <div class="logo">
            <a href="/" title="PhpBy个人轻博客"><img src="/static/index/images/logoko.png" alt="PhpBy个人轻博客">PhpBy个人轻博客</a>
        </div>
        <ul class="nav">
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-237 current-menu-item">
                <a href="#">1.1</a>
            </li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
                <a href="#">1.2</a>
            </li>
        </ul>
        <form method="get" class="search-form" action="http://code.py40.com/"><input class="form-control" name="s" type="text" placeholder="输入关键字" value=""><input class="btn" type="submit" value="搜索"></form>
        <div class="feeds"></div>
        <div class="slinks">
            <a href="http://code.py40.com/changelinkes">交换友链</a>|<a href="http://code.py40.com/126.html">关于我们</a>
            <br>
            <a href="http://sport.py40.com/" target="_blank">猫友运动</a>|<a target="_blank" href="http://mm.py40.com/">猫友图库</a>
        </div>
    </header>
    <div class="content-wrap">
        <div class="content">

            <header class="article-header">
                <div class="breadcrumbs"><span class="text-muted">当前位置：</span><a href="http://code.py40.com">Python在线学习</a> <small>&gt;</small> <a href="http://code.py40.com/category/asc3">Python爬虫</a> <small>&gt;</small> <span class="text-muted">正文</span></div>
                <h1 class="article-title"><a href="http://code.py40.com/2998.html">Python爬虫提高之异常处理</a></h1>
                <ul class="article-meta">
                    <li>拼命三郎 发布于 2017-06-06</li>
                    <li>分类：<a href="http://code.py40.com/category/asc3" rel="category tag" data-original-title="" title="">Python爬虫</a></li>
                    <li><span class="post-views">阅读(930)</span></li>
                    <li>评论(0)</li>
                    <li></li>
                </ul>
            </header>

            <article class="article-content">
                <script type="text/javascript">
                    $(function() {
                        var ie6 = document.all;
                        var dv = $('#article-index'),st;
                        dv.attr('otop', dv.offset().top); //存储原来的距离顶部的距离
                        console.log(dv.offset().left);
                        dv.attr('oleft', dv.offset().left); //存储原来的距离顶部的距离
                        $(window).scroll(function() {
                            st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
                            if (st > parseInt(dv.attr('otop'))) {
                                if (ie6) { //IE6不支持fixed属性，所以只能靠设置position为absolute和top实现此效果
                                    dv.css({
                                        position: 'absolute',
                                        top: st,
                                        left:dv.offset().left
                                    });
                                } else if (dv.css('position') != 'fixed') dv.css({
                                    'position': 'fixed',
                                    top: 0,
                                    left:dv.offset().left
                                });
                            } else if (dv.css('position') != 'static') dv.css({
                                'position': 'static'

                            });
                        });
                    });

                    $(function() {
                        $(text_yincang).click(function(){
                            var target=document.getElementById('text_yincang');
                            var goal = document.getElementById('index-ul');

                            if (goal.style.display=="block"){
                                goal.style.display="none";
                                target.innerText=" [ 显示 ] ";

                            } else {
                                goal.style.display="block";
                                target.innerText=' [ 隐藏 ] ';
                            }
                        });
                    });

                </script>



                <div id="article-index" otop="183" oleft="827.5" style="top: 0px; left: 827.5px; position: static;"><strong>文章目录</strong><text id="text_yincang"> [ 隐藏 ] </text>
                    <ul id="index-ul" style="display:block">
                        <li><a href="#title-0">Python爬虫框架之异常处理</a></li><li><a href="#title-1">一：基础try except异常处理</a></li><li><a href="#title-2">二：请求函数的超时处理</a></li><li><a href="#title-3">三：selenium+chrome  | phantomjs 的超时处理</a></li><li><a href="#title-4">四：自定义函数的死锁or超时处理</a></li><li><a href="#title-5">五：自定义线程的死锁or超时处理</a></li><li><a href="#title-6">六：程序自动重启</a></li></ul>
                </div>
                <h2 id="title-0">Python爬虫框架之异常处理</h2>
                <p>任何访问服务器获取数据的请求，都需要做异常处理，当然爬虫更需要我们对各种异常进行处理。只有这样才能提高爬虫的健壮性。如果我们的爬虫足够健壮，那么就能确保程序几个月不停止。</p>
                <p>我们从以下几个方面做出讲解：</p>
                <ul>
                    <li>一：try except处理异常代码块</li>
                    <li>二：普通请求函数的超时处理</li>
                    <li>三：selenium+chrome  | phantomjs 的超时处理</li>
                    <li>四：自定义函数的死锁or超时处理</li>
                    <li>五：自定义线程的死锁or超时处理</li>
                    <li>六：自重启的程序设计</li>
                </ul>
                <h2 id="title-1">一：基础try except异常处理</h2>
                <p>try except的语句可以让我们的程序跳过代码中可能出现的异常</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573a91609067113" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">try:
    pass http://code.py40.com/deliver-article/#
    #可能出错的语句
except Exception as e:
    pass
    #保留错误的url，留待下次重跑
    print(e)
finally:
    #无论是否处理了异常都继续运行
    print(time.ctime())</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573a91609067113-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573a91609067113-2">2</div><div class="crayon-num" data-line="crayon-5afdade573a91609067113-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573a91609067113-4">4</div><div class="crayon-num" data-line="crayon-5afdade573a91609067113-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573a91609067113-6">6</div><div class="crayon-num" data-line="crayon-5afdade573a91609067113-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573a91609067113-8">8</div><div class="crayon-num" data-line="crayon-5afdade573a91609067113-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573a91609067113-10">10</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573a91609067113-1"><span class="crayon-st">try</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573a91609067113-2"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-r">pass</span><span class="crayon-h"> </span><span class="crayon-v">http</span><span class="crayon-o">:</span><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-k ">code</span><span class="crayon-sy">.</span><span class="crayon-v">py40</span><span class="crayon-sy">.</span><span class="crayon-v">com</span><span class="crayon-o">/</span><span class="crayon-v">deliver</span><span class="crayon-o">-</span><span class="crayon-v">article</span><span class="crayon-o">/</span><span class="crayon-c">#</span></div><div class="crayon-line" id="crayon-5afdade573a91609067113-3"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-c">#可能出错的语句</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573a91609067113-4"><span class="crayon-st">except</span><span class="crayon-h"> </span><span class="crayon-k ">Exception</span><span class="crayon-h"> </span><span class="crayon-st">as</span><span class="crayon-h"> </span><span class="crayon-v">e</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573a91609067113-5"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-r">pass</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573a91609067113-6"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-c">#保留错误的url，留待下次重跑</span></div><div class="crayon-line" id="crayon-5afdade573a91609067113-7"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">e</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573a91609067113-8"><span class="crayon-st">finally</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573a91609067113-9"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-c">#无论是否处理了异常都继续运行</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573a91609067113-10"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-k ">time</span><span class="crayon-sy">.</span><span class="crayon-e">ctime</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0017 seconds] -->
                <p> </p>
                <h2 id="title-2">二：请求函数的超时处理</h2>
                <p>2.1:普通请求：</p>
                <p>2.1.1单请求类型：</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573aa4555395053" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; overflow: hidden; z-index: 0;">import requests
requests.get(url,timeout=60)</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -18px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573aa4555395053-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aa4555395053-2">2</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573aa4555395053-1"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">requests</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aa4555395053-2"><span class="crayon-v">requests</span><span class="crayon-sy">.</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-v">url</span><span class="crayon-sy">,</span><span class="crayon-v">timeout</span><span class="crayon-o">=</span><span class="crayon-cn">60</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0005 seconds] -->
                <p> </p>
                <p>2.1.2会话保持类型：</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573aa8016344491" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; overflow: hidden; z-index: 0;">import requesocks
session = requesocks.session()
response = session.get(URL,headers=headers,timeout=10)</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -18px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573aa8016344491-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aa8016344491-2">2</div><div class="crayon-num" data-line="crayon-5afdade573aa8016344491-3">3</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573aa8016344491-1"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">requesocks</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aa8016344491-2"><span class="crayon-v">session</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">requesocks</span><span class="crayon-sy">.</span><span class="crayon-e">session</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573aa8016344491-3"><span class="crayon-v">response</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">session</span><span class="crayon-sy">.</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-v">URL</span><span class="crayon-sy">,</span><span class="crayon-v">headers</span><span class="crayon-o">=</span><span class="crayon-v">headers</span><span class="crayon-sy">,</span><span class="crayon-v">timeout</span><span class="crayon-o">=</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0008 seconds] -->
                <p> </p>
                <h2 id="title-3">三：selenium+chrome  | phantomjs 的超时处理</h2>
                <p>2.2.1：selenium+chrome的超时设置</p>
                <p>官网原文：http://selenium-python.readthedocs.io/waits.html</p>
                <p>显式等待：、等待某个条件发生，然后再继续进行代码。</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573aab402966474" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(  #这里修改时间
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573aab402966474-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-2">2</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-4">4</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-6">6</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-8">8</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-10">10</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-11">11</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aab402966474-12">12</div><div class="crayon-num" data-line="crayon-5afdade573aab402966474-13">13</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573aab402966474-1"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-e">selenium </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">webdriver</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-2"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-v">selenium</span><span class="crayon-sy">.</span><span class="crayon-v">webdriver</span><span class="crayon-sy">.</span><span class="crayon-v">common</span><span class="crayon-sy">.</span><span class="crayon-e">by </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">By</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-3"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-v">selenium</span><span class="crayon-sy">.</span><span class="crayon-v">webdriver</span><span class="crayon-sy">.</span><span class="crayon-v">support</span><span class="crayon-sy">.</span><span class="crayon-e">ui </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">WebDriverWait</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-4"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-v">selenium</span><span class="crayon-sy">.</span><span class="crayon-v">webdriver</span><span class="crayon-sy">.</span><span class="crayon-e">support </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">expected_conditions </span><span class="crayon-st">as</span><span class="crayon-h"> </span><span class="crayon-e">EC</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-5"><span class="crayon-e"> </span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-6"><span class="crayon-v">driver</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">webdriver</span><span class="crayon-sy">.</span><span class="crayon-e">Firefox</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-7"><span class="crayon-v">driver</span><span class="crayon-sy">.</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-s">"http://somedomain/url_that_delays_loading"</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-8"><span class="crayon-st">try</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-9"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">element</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">WebDriverWait</span><span class="crayon-sy">(</span><span class="crayon-v">driver</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">until</span><span class="crayon-sy">(</span><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-c">#这里修改时间</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-10"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">EC</span><span class="crayon-sy">.</span><span class="crayon-e">presence_of_element_located</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-v">By</span><span class="crayon-sy">.</span><span class="crayon-k ">ID</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">"myDynamicElement"</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-11"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aab402966474-12"><span class="crayon-st">finally</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573aab402966474-13"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">driver</span><span class="crayon-sy">.</span><span class="crayon-e">quit</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0023 seconds] -->
                <p> </p>
                <p>隐式等待：是告诉WebDriver在尝试查找一个或多个元素（如果它们不是立即可用的）时轮询DOM一定时间。默认设置为0，一旦设置，将为WebDriver对象实例的生命期设置隐式等待。</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573aad972141406" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -18px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573aad972141406-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aad972141406-2">2</div><div class="crayon-num" data-line="crayon-5afdade573aad972141406-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aad972141406-4">4</div><div class="crayon-num" data-line="crayon-5afdade573aad972141406-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573aad972141406-6">6</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573aad972141406-1"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-e">selenium </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">webdriver</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aad972141406-2"><span class="crayon-e"> </span></div><div class="crayon-line" id="crayon-5afdade573aad972141406-3"><span class="crayon-v">driver</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">webdriver</span><span class="crayon-sy">.</span><span class="crayon-e">Firefox</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aad972141406-4"><span class="crayon-v">driver</span><span class="crayon-sy">.</span><span class="crayon-e">implicitly_wait</span><span class="crayon-sy">(</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-c"># seconds</span></div><div class="crayon-line" id="crayon-5afdade573aad972141406-5"><span class="crayon-v">driver</span><span class="crayon-sy">.</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-s">"http://somedomain/url_that_delays_loading"</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573aad972141406-6"><span class="crayon-v">myDynamicElement</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">driver</span><span class="crayon-sy">.</span><span class="crayon-e">find_element_by_id</span><span class="crayon-sy">(</span><span class="crayon-s">"myDynamicElement"</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0010 seconds] -->
                <p> </p>
                <p>2.2.2：phantomjs的超时设置</p>
                <p>这里使用不带selenium的phantomjs，需要使用js。主要设置语句是</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573ab0280299819" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">#JavaScript

page.settings.resourceTimeout = 5000; // 等待5秒

var system = require('system');
var args = system.args;
var url = args[1];
var page = require('webpage').create();
page.settings.resourceTimeout = 5000; // 等待5秒
page.onResourceTimeout = function(e) {
console.log(e.errorCode);&amp;nbsp;&amp;nbsp; //打印错误码
console.log(e.errorString);//打印错误语句
console.log(e.url); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//打印错误url
phantom.exit(1);
};
page.open(url, function(status) {
if(status==='success'){
var html=page.evaluate(function(){
returndocument.documentElement.outerHTML;
});
console.log(html);
}
phantom.exit();
});
//$phantomjs xx.js http://bbs.pcbaby.com.cn/topic-2149414.html</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-2">2</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-4">4</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-6">6</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-8">8</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-10">10</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-11">11</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-12">12</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-13">13</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-14">14</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-15">15</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-16">16</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-17">17</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-18">18</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-19">19</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-20">20</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-21">21</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-22">22</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-23">23</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab0280299819-24">24</div><div class="crayon-num" data-line="crayon-5afdade573ab0280299819-25">25</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573ab0280299819-1"><span class="crayon-c">#JavaScript</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-2">&nbsp;</div><div class="crayon-line" id="crayon-5afdade573ab0280299819-3"><span class="crayon-v">page</span><span class="crayon-sy">.</span><span class="crayon-v">settings</span><span class="crayon-sy">.</span><span class="crayon-v">resourceTimeout</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">5000</span><span class="crayon-sy">;</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-h"> </span>等待<span class="crayon-cn">5</span>秒</div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-4">&nbsp;</div><div class="crayon-line" id="crayon-5afdade573ab0280299819-5"><span class="crayon-e">var </span><span class="crayon-v">system</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'system'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-6"><span class="crayon-e">var </span><span class="crayon-v">args</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">system</span><span class="crayon-sy">.</span><span class="crayon-v">args</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-7"><span class="crayon-e">var </span><span class="crayon-v">url</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">args</span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-8"><span class="crayon-e">var </span><span class="crayon-v">page</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'webpage'</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">create</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-9"><span class="crayon-v">page</span><span class="crayon-sy">.</span><span class="crayon-v">settings</span><span class="crayon-sy">.</span><span class="crayon-v">resourceTimeout</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">5000</span><span class="crayon-sy">;</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-h"> </span>等待<span class="crayon-cn">5</span>秒</div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-10"><span class="crayon-v">page</span><span class="crayon-sy">.</span><span class="crayon-v">onResourceTimeout</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">function</span><span class="crayon-sy">(</span><span class="crayon-v">e</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-11"><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">e</span><span class="crayon-sy">.</span><span class="crayon-v">errorCode</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-o">/</span>打印错误码</div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-12"><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">e</span><span class="crayon-sy">.</span><span class="crayon-v">errorString</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-o">/</span><span class="crayon-o">/</span>打印错误语句</div><div class="crayon-line" id="crayon-5afdade573ab0280299819-13"><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">e</span><span class="crayon-sy">.</span><span class="crayon-v">url</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h"> </span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-o">&amp;</span><span class="crayon-v">nbsp</span><span class="crayon-sy">;</span><span class="crayon-o">/</span><span class="crayon-o">/</span>打印错误<span class="crayon-e">url</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-14"><span class="crayon-v">phantom</span><span class="crayon-sy">.</span><span class="crayon-e">exit</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-15"><span class="crayon-sy">}</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-16"><span class="crayon-v">page</span><span class="crayon-sy">.</span><span class="crayon-k ">open</span><span class="crayon-sy">(</span><span class="crayon-v">url</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">function</span><span class="crayon-sy">(</span><span class="crayon-v">status</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-17"><span class="crayon-st">if</span><span class="crayon-sy">(</span><span class="crayon-v">status</span><span class="crayon-o">===</span><span class="crayon-s">'success'</span><span class="crayon-sy">)</span><span class="crayon-sy">{</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-18"><span class="crayon-e">var </span><span class="crayon-v">html</span><span class="crayon-o">=</span><span class="crayon-v">page</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-e">function</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-19"><span class="crayon-v">returndocument</span><span class="crayon-sy">.</span><span class="crayon-v">documentElement</span><span class="crayon-sy">.</span><span class="crayon-v">outerHTML</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-20"><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-21"><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">html</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-22"><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-23"><span class="crayon-v">phantom</span><span class="crayon-sy">.</span><span class="crayon-e">exit</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab0280299819-24"><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5afdade573ab0280299819-25"><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-sy">$</span><span class="crayon-e">phantomjs </span><span class="crayon-v">xx</span><span class="crayon-sy">.</span><span class="crayon-e">js </span><span class="crayon-v">http</span><span class="crayon-o">:</span><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-v">bbs</span><span class="crayon-sy">.</span><span class="crayon-v">pcbaby</span><span class="crayon-sy">.</span><span class="crayon-v">com</span><span class="crayon-sy">.</span><span class="crayon-v">cn</span><span class="crayon-o">/</span><span class="crayon-v">topic</span><span class="crayon-o">-</span><span class="crayon-cn">2149414.html</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0049 seconds] -->
                <p> </p>
                <h2 id="title-4">四：自定义函数的死锁or超时处理</h2>
                <p>这个非常重要！！</p>
                <p>python是顺序执行的，但是如果下一句话可能导致死锁（比如一个while（1））那么如何强制让他超时呢？他本身如果没有带有超时设置的话，就要自己运行信号（import signal）来处理</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573ab3481156809" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">#coding:utf-8
import time
import signal

def test(i):
    time.sleep(0.999)#模拟超时的情况
    print "%d within time"%(i)
    return i

def fuc_time(time_out):
    # 此为函数超时控制，替换下面的test函数为可能出现未知错误死锁的函数
    def handler(signum, frame):
        raise AssertionError
    try:
        signal.signal(signal.SIGALRM, handler)
        signal.alarm(time_out)#time_out为超时时间
        temp = test(1) #函数设置部分，如果未超时则正常返回数据，
        return temp
    except AssertionError:
        print("%d timeout"%(i))# 超时则报错

if __name__ == '__main__':
    for i in range(1,10):
        fuc_time(1)</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-2">2</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-4">4</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-6">6</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-8">8</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-10">10</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-11">11</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-12">12</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-13">13</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-14">14</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-15">15</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-16">16</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-17">17</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-18">18</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-19">19</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-20">20</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-21">21</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-22">22</div><div class="crayon-num" data-line="crayon-5afdade573ab3481156809-23">23</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab3481156809-24">24</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573ab3481156809-1"><span class="crayon-c">#coding:utf-8</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-2"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">time</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-3"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">signal</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-4"><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-5"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-k ">test</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-6"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">time</span><span class="crayon-sy">.</span><span class="crayon-e">sleep</span><span class="crayon-sy">(</span><span class="crayon-cn">0.999</span><span class="crayon-sy">)</span><span class="crayon-c">#模拟超时的情况</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-7"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-h"> </span><span class="crayon-s">"%d within time"</span><span class="crayon-o">%</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-8"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">i</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-9"><span class="crayon-h"> </span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-10"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">fuc_time</span><span class="crayon-sy">(</span><span class="crayon-v">time_out</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-11"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-c"># 此为函数超时控制，替换下面的test函数为可能出现未知错误死锁的函数</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-12"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">handler</span><span class="crayon-sy">(</span><span class="crayon-v">signum</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">frame</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-13"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">raise</span><span class="crayon-h"> </span><span class="crayon-k ">AssertionError</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-14"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">try</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-15"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">signal</span><span class="crayon-sy">.</span><span class="crayon-k ">signal</span><span class="crayon-sy">(</span><span class="crayon-k ">signal</span><span class="crayon-sy">.</span><span class="crayon-v">SIGALRM</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">handler</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-16"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">signal</span><span class="crayon-sy">.</span><span class="crayon-e">alarm</span><span class="crayon-sy">(</span><span class="crayon-v">time_out</span><span class="crayon-sy">)</span><span class="crayon-c">#time_out为超时时间</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-17"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">temp</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-k ">test</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-c">#函数设置部分，如果未超时则正常返回数据，</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-18"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-e">temp</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-19"><span class="crayon-e">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">except</span><span class="crayon-h"> </span><span class="crayon-k ">AssertionError</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-20"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">"%d timeout"</span><span class="crayon-o">%</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-c"># 超时则报错</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-21"><span class="crayon-h"> </span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-22"><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-v">__name__</span><span class="crayon-h"> </span><span class="crayon-o">==</span><span class="crayon-h"> </span><span class="crayon-s">'__main__'</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab3481156809-23"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">i</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-k ">range</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab3481156809-24"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-e">fuc_time</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0031 seconds] -->
                <p> </p>
                <h2 id="title-5">五：自定义线程的死锁or超时处理</h2>
                <p>在某个程序中一方面不适合使用selenium+phantomjs的方式（要实现的功能比较难不适合）因为只能用原生的phantomjs，但是这个问题他本身在极端情况下也有可能停止（在超时设置之前因为某些错误）</p>
                <p>那么最佳方案就是用python单独开一个线程（进程）调用原生phantomjs，然后对这个线程进程进行超时控制。</p>
                <p>这里用ping这个命令先做测试，</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573ab6920974905" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; opacity: 0; z-index: 0; overflow: hidden;">import subprocess
from threading import Timer
import time

kill = lambda process: process.kill()

cmd = ["ping", "www.google.com"]
ping = subprocess.Popen(
    cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

my_timer = Timer(5, kill, [ping])#这里设定时间，和命令
try:
    my_timer.start()#启用
    stdout, stderr = ping.communicate()#获得输出
    #print stderr
    print(time.ctime())
finally:
    print(time.ctime())
    my_timer.cancel()</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-2">2</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-4">4</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-6">6</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-8">8</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-10">10</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-11">11</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-12">12</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-13">13</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-14">14</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-15">15</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-16">16</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-17">17</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab6920974905-18">18</div><div class="crayon-num" data-line="crayon-5afdade573ab6920974905-19">19</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573ab6920974905-1"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">subprocess</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-2"><span class="crayon-st">from</span><span class="crayon-h"> </span><span class="crayon-k ">threading</span><span class="crayon-h"> </span><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">Timer</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-3"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">time</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-4"><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-5"><span class="crayon-v">kill</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">process</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">process</span><span class="crayon-sy">.</span><span class="crayon-e">kill</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-6"><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-7"><span class="crayon-k ">cmd</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-s">"ping"</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">"www.google.com"</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-8"><span class="crayon-v">ping</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-k ">subprocess</span><span class="crayon-sy">.</span><span class="crayon-e">Popen</span><span class="crayon-sy">(</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-9"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">cmd</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">stdout</span><span class="crayon-o">=</span><span class="crayon-k ">subprocess</span><span class="crayon-sy">.</span><span class="crayon-v">PIPE</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">stderr</span><span class="crayon-o">=</span><span class="crayon-k ">subprocess</span><span class="crayon-sy">.</span><span class="crayon-v">PIPE</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-10"><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-11"><span class="crayon-v">my_timer</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Timer</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kill</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-v">ping</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-c">#这里设定时间，和命令</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-12"><span class="crayon-st">try</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-13"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">my_timer</span><span class="crayon-sy">.</span><span class="crayon-e">start</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-c">#启用</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-14"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">stdout</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">stderr</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">ping</span><span class="crayon-sy">.</span><span class="crayon-e">communicate</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-c">#获得输出</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-15"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-c">#print stderr</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-16"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-k ">time</span><span class="crayon-sy">.</span><span class="crayon-e">ctime</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-17"><span class="crayon-st">finally</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab6920974905-18"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-k ">time</span><span class="crayon-sy">.</span><span class="crayon-e">ctime</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573ab6920974905-19"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">my_timer</span><span class="crayon-sy">.</span><span class="crayon-e">cancel</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0029 seconds] -->
                <p> </p>
                <h2 id="title-6">六：程序自动重启</h2>
                <p>比如我们的程序在某种情况下报错多次，那么当满足条件后，让其重启即可解决大多数问题，当然这只不过是治标不治本而已，如果这个程序重启没有大问题（例如读队列类型）那么自重启这是最省力的方式之一。</p>
                <p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

                <div id="crayon-5afdade573ab9796870218" class="crayon-syntax crayon-theme-sublime-text crayon-font-consolas crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover" style="margin: 12px; font-size: 12px !important; line-height: 20px !important; height: auto;">

                    <div class="crayon-plain-wrap"><textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 12px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">import time
import sys
import os
def restart_program():
  python = sys.executable
  os.execl(python, python, * sys.argv)

if __name__ == "__main__":
  print 'start...'
  print u"3秒后,程序将结束...".encode("utf8")
  time.sleep(3)
  restart_program()</textarea></div>
                    <div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
                        <table class="crayon-table" style="margin-left: -25px;">
                            <tbody><tr class="crayon-row">
                                <td class="crayon-nums " data-settings="hide">
                                    <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-1">1</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-2">2</div><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-3">3</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-4">4</div><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-5">5</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-6">6</div><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-7">7</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-8">8</div><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-9">9</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-10">10</div><div class="crayon-num" data-line="crayon-5afdade573ab9796870218-11">11</div><div class="crayon-num crayon-striped-num" data-line="crayon-5afdade573ab9796870218-12">12</div></div>
                                </td>
                                <td class="crayon-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5afdade573ab9796870218-1"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">time</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-2"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">sys</span></div><div class="crayon-line" id="crayon-5afdade573ab9796870218-3"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-k ">os</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-4"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">restart_program</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab9796870218-5"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">python</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-k ">sys</span><span class="crayon-sy">.</span><span class="crayon-e">executable</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-6"><span class="crayon-e">&nbsp;&nbsp;</span><span class="crayon-k ">os</span><span class="crayon-sy">.</span><span class="crayon-e">execl</span><span class="crayon-sy">(</span><span class="crayon-v">python</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">python</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-k ">sys</span><span class="crayon-sy">.</span><span class="crayon-v">argv</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573ab9796870218-7"><span class="crayon-h">&nbsp;&nbsp;</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-8"><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-v">__name__</span><span class="crayon-h"> </span><span class="crayon-o">==</span><span class="crayon-h"> </span><span class="crayon-s">"__main__"</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5afdade573ab9796870218-9"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-h"> </span><span class="crayon-s">'start...'</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-10"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-k ">print</span><span class="crayon-h"> </span><span class="crayon-i">u</span><span class="crayon-s">"3秒后,程序将结束..."</span><span class="crayon-sy">.</span><span class="crayon-e">encode</span><span class="crayon-sy">(</span><span class="crayon-s">"utf8"</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5afdade573ab9796870218-11"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-k ">time</span><span class="crayon-sy">.</span><span class="crayon-e">sleep</span><span class="crayon-sy">(</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="crayon-5afdade573ab9796870218-12"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-e">restart_program</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td>
                            </tr>
                            </tbody></table>
                    </div>
                </div>
                <!-- [Format Time: 0.0017 seconds] -->
                <p> </p>
                <p class="post-copyright">未经允许不得转载：<a href="http://code.py40.com">Python在线学习</a> » <a href="http://code.py40.com/2998.html">Python爬虫提高之异常处理</a></p>		</article>
        </div>
    </div>

    <aside class="sidebar">
        <div class="widget widget_text">
            <h3 class="title"><strong>在线手册中心</strong></h3>
            <div class="textwidget">
                <ul>
                    <li style="padding:0px 0px 10px 0px;">
                        <a target="_blank" href="http://code.py40.com/python3"><span class="glyphicon glyphicon-share"></span> Python3中文教程</a>
                    </li>
                    <li style="padding:0px 0px 10px 0px;">
                        <a target="_blank" href="http://code.py40.com/pyqt5"><span class="glyphicon glyphicon-share"></span> PyQt5中文教程</a>
                    </li>
                    <li style="padding:0px 0px 10px 0px;">
                        <a target="_blank" href="http://code.py40.com/algorithm"><span class="glyphicon glyphicon-share"></span> Python算法</a>
                    </li><li style="padding:0px 0px 10px 0px;">
                    <a target="_blank" href="http://code.py40.com/pythongame"><span class="glyphicon glyphicon-share"></span> Python游戏开发</a>
                </li>
                    <li style="padding:0px 0px 10px 0px;">
                        <a target="_blank" href="http://code.py40.com/pythonmodule"><span class="glyphicon glyphicon-share"></span> Python模块学习</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="widget widget_tags">
            <h3 class="title"><strong>热门标签</strong></h3>
            <ul class="widget_tags_inner">
                <li><a title="" href="http://code.py40.com/tag/android%e5%9f%ba%e7%a1%80" data-original-title="13个话题">Android基础</a></li>
                <li><a title="" href="http://code.py40.com/tag/python3%e6%95%99%e7%a8%8b" data-original-title="5个话题">python3教程</a></li>
                <li><a title="" href="http://code.py40.com/tag/python3%e7%88%ac%e8%99%ab" data-original-title="5个话题">python3爬虫</a></li>
                <li><a title="" href="http://code.py40.com/tag/python%e8%b5%84%e6%ba%90" data-original-title="5个话题">Python资源</a></li>
                <li><a title="" href="http://code.py40.com/tag/android%e7%bc%96%e8%af%91%e5%b7%a5%e5%85%b7" data-original-title="5个话题">Android编译工具</a></li>
                <li><a title="" href="http://code.py40.com/tag/python3%e5%bc%82%e5%b8%b8" data-original-title="4个话题">python3异常</a></li>
                <li><a title="" href="http://code.py40.com/tag/python3%e5%ba%93" data-original-title="4个话题">python3库</a></li>
                <li><a title="" href="http://code.py40.com/tag/python%e6%a8%a1%e5%9d%97" data-original-title="4个话题">python模块</a></li>
                <li><a title="" href="http://code.py40.com/tag/python%e6%b8%b8%e6%88%8f%e5%bc%80%e5%8f%91" data-original-title="4个话题">Python游戏开发</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e5%b7%a5%e5%85%b7" data-original-title="4个话题">工具</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e4%b8%aa%e4%ba%ba%e5%8d%9a%e5%ae%a2" data-original-title="3个话题">个人博客</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e8%87%aa%e5%8a%a8%e5%8c%96%e8%84%9a%e6%9c%ac" data-original-title="3个话题">自动化脚本</a></li>
                <li><a title="" href="http://code.py40.com/tag/linux" data-original-title="3个话题">Linux</a></li>
                <li><a title="" href="http://code.py40.com/tag/android%e8%87%aa%e5%ae%9a%e4%b9%89view" data-original-title="3个话题">Android自定义View</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e8%84%9a%e6%9c%ac%e5%85%a5%e9%97%a8" data-original-title="2个话题">脚本入门</a></li>
                <li><a title="" href="http://code.py40.com/tag/mac%e6%8a%80%e5%b7%a7" data-original-title="2个话题">mac技巧</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f" data-original-title="2个话题">微信小程序</a></li>
                <li><a title="" href="http://code.py40.com/tag/andoid%e5%b0%8f%e6%8a%80%e5%b7%a7" data-original-title="2个话题">Andoid小技巧</a></li>
                <li><a title="" href="http://code.py40.com/tag/python3%e5%ae%9e%e4%be%8b" data-original-title="1个话题">python3实例</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e6%b8%b8%e6%88%8f" data-original-title="1个话题">游戏</a></li>
                <li><a title="" href="http://code.py40.com/tag/pyqt5" data-original-title="1个话题">PyQt5</a></li>
                <li><a title="" href="http://code.py40.com/tag/centos" data-original-title="1个话题">Centos</a></li>
                <li><a title="" href="http://code.py40.com/tag/%e7%88%ac%e8%99%ab%e5%85%a5%e9%97%a8" data-original-title="1个话题">爬虫入门</a></li>
                <li><a title="" href="http://code.py40.com/tag/python" data-original-title="1个话题">python</a></li>
            </ul>
        </div>
        <div class="widget widget_postlist">
            <h3 class="title"><strong>随机推荐</strong></h3>
            <ul class="items-01">
                <li>
                    <a href="http://code.py40.com/2971.html">
					<span class="thumbnail">
						<img data-original="http://code.py40.com/wp-content/uploads/2017/05/jd1.bmp" class="thumb" src="/static/index/images/thumbnail.png">
					</span>
                        <span class="text">python爬取京东数据</span>
                        <span class="text-muted post-views">阅读(1262)</span>
                    </a>
                </li>
                <li>
                    <a href="http://code.py40.com/2971.html">
					<span class="thumbnail">
						<img data-original="http://code.py40.com/wp-content/uploads/2017/05/jd1.bmp" class="thumb" src="/static/index/images/thumbnail.png">
					</span>
                        <span class="text">python爬取京东数据</span>
                        <span class="text-muted post-views">阅读(1262)</span>
                    </a>
                </li>
                <li>
                    <a href="http://code.py40.com/2971.html">
					<span class="thumbnail">
						<img data-original="http://code.py40.com/wp-content/uploads/2017/05/jd1.bmp" class="thumb" src="/static/index/images/thumbnail.png">
					</span>
                        <span class="text">python爬取京东数据</span>
                        <span class="text-muted post-views">阅读(1262)</span>
                    </a>
                </li>
                <li>
                    <a href="http://code.py40.com/2971.html">
					<span class="thumbnail">
						<img data-original="http://code.py40.com/wp-content/uploads/2017/05/jd1.bmp" class="thumb" src="/static/index/images/thumbnail.png">
					</span>
                        <span class="text">python爬取京东数据</span>
                        <span class="text-muted post-views">阅读(1262)</span>
                    </a>
                </li>
            </ul>
        </div>
    </aside>
    <footer class="footer">
        <div class="flinks">
            <strong>合作伙伴</strong>
            <ul class="xoxo blogroll">
                <li><a href="http://www.monkeyblog.cn/" target="_blank">monkey前端博客</a></li>
                <li><a href="http://wordpressleaf.com/" target="_blank">WordpressLeaf</a></li>
                <li><a href="http://8e8z.com/" target="_blank">绿叶之秋</a></li>
                <li><a href="http://sport.py40.com/" target="_blank">跑趣吧</a></li>
                <li><a href="http://www.sxlog.com/" target="_blank">随心网</a></li>
                <li><a href="http://tianziheng.top/" target="_blank">田梓衡的博客</a></li>
                <li><a href="http://www.xyzbeta.com/" target="_blank">维简网</a></li>
                <li><a href="http://www.xueshanlinghu.com/" target="_blank">雪山凌狐教程</a></li>
                <li><a href="https://www.1314mylove.com/" target="_blank">壹叁壹肆</a></li>
                <li><a href="http://www.yxpk.net/" target="_blank">影戏拍客</a></li>
            </ul>
        </div>
        © 2018 <a href="http://code.py40.com/">Python在线学习</a> &nbsp; 本站主题由 <a href="http://py40.com/" target="_blank">py40.com </a> 提供 &nbsp;
        <!--<div style="display:none">-->
        <!--<script language="javascript" type="text/javascript" src="/static/index/19217049.js"></script>-->
        <!--<a href="https://www.51.la/?comId=19217049" title="51.La 网站流量统计系统" target="_blank">-->
        <!--<span style="display:inline-block;background-color:#EF5350;color:#fff;padding:2px 5px;font-family:arial;font-size:12px;font-weight:bold;">51La</span>-->
        <!--</a>-->
        <!--</div>-->
        <div class="top25">
            Python在线学习<span id="span_dt_dt">已运行： 610天 9小时 20分 27秒  未来的日子我陪你一起走过</span>
        </div>
        <script>
            function show_date_time() {
                window.setTimeout("show_date_time()", 1000);
                BirthDay = new Date("9/1/2016 00:00:01");
                today = new Date();
                timeold = (today.getTime() - BirthDay.getTime());
                sectimeold = timeold / 1000
                secondsold = Math.floor(sectimeold);
                msPerDay = 24 * 60 * 60 * 1000
                e_daysold = timeold / msPerDay
                daysold = Math.floor(e_daysold);
                e_hrsold = (e_daysold - daysold) * 24;
                hrsold = Math.floor(e_hrsold);
                e_minsold = (e_hrsold - hrsold) * 60;
                minsold = Math.floor((e_hrsold - hrsold) * 60);
                seconds = Math.floor((e_minsold - minsold) * 60);
                span_dt_dt.innerHTML = "已运行： " + daysold + "天 " + hrsold + "小时 " + minsold + "分 " + seconds + "秒 " +" 未来的日子我陪你一起走过";
            }
            show_date_time();
        </script>
    </footer>
</section>
</body>
</html>